<?php

/**
 * Class AssetsController
 */
class AssetsController extends Controller_Base
{
    public function init()
    {
        parent::init();
        SmartyAdapter::assign('accessType', Module_InfoBase_Assets::$accessType);
    }

    /**
     * 资产方列表
     */
    public function indexAction()
    {
        $request = $this->getRequest();
        $pn = intval($request->getQuery('page', 1));
        $pageQuery = [];
        $whereQuery = [];
        $pn = $pn ? $pn : 1;
        $pageSize = 10;
        $start = ($pn - 1) * $pageSize;
        $total = Module_InfoBase_Assets::getInstance()->getAllBySearch(['count(id) as total'], ['where' => $whereQuery]);
        $total = $total[0]['total'];
        //计算分页
        $totalPage = ceil($total / $pageSize);
        if ($pn > $totalPage && $totalPage > 0) {
            $this->redirect('/assets/index?page=' . $totalPage);
            exit;
        }
        $list = Module_InfoBase_Assets::getInstance()->getAllBySearch(['*'], array('where' => $whereQuery, 'start' => $start, 'order' => 'id DESC', 'limit' => 10));
        SmartyAdapter::assign('list', $list);
        SmartyAdapter::assign('totalPage', $totalPage);
        SmartyAdapter::assign('totalnum', $total);
        SmartyAdapter::assign('pagesize', $pageSize);
        SmartyAdapter::assign('curPageNum', $pn);
        SmartyAdapter::assign('numCard', Module_Mediate_Workernumber::$numCard);
        SmartyAdapter::assign('page_query', http_build_query($pageQuery));
        SmartyAdapter::assign('smsSign', Lib_SMS_Config::$sms_sign);
        SmartyAdapter::assign('centerName', Lib_SMS_Config::$centen_name);
        SmartyAdapter::$templateName = 'assets/index';
    }

    /**
     * 删除资产方
     */
    public function delAction()
    {
        $request = $this->getRequest();
        $id = intval($request->getPost('id'));
        if (!$id) {
            AjaxTool::returnJsonData(4001, Setup_Error::getConfig(4001), []);
        }
        $firm = Module_InfoBase_Assets::getInstance()->getItemById($id);
        if (!$firm) {
            AjaxTool::returnJsonData(4005,'找不到资产方信息', []);
        }
        $res = Module_InfoBase_Assets::getInstance()->deleteItemById($id);
        if (!$res) {
            AjaxTool::returnJsonData(4000, '删除失败', []);
        }
        AjaxTool::returnJsonData(200, Setup_Error::getConfig(200), []);
    }

    /**
     * 添加资产方
     */
    public function addAction() {
        SmartyAdapter::assign('callNum', Module_Mediate_Workernumber::$callNum[1]);
        SmartyAdapter::assign('callNum2', Module_Mediate_Workernumber::$callNum[2]);
        SmartyAdapter::assign('numCard', Module_Mediate_Workernumber::$numCard);
        SmartyAdapter::assign('smsSign', Lib_SMS_Config::$sms_sign);
        SmartyAdapter::assign('centerName', Lib_SMS_Config::$centen_name);
        SmartyAdapter::$templateName = 'assets/addandedit';

    }

    /**
     *修改或编辑
     */
    public function addAndEditAction() {
        $id = $this->getRequest()->getPost('id', 0);
        $post_data = $this->getRequest()->getPost('assets', []);

        if (empty($post_data['name'])) {
            AjaxTool::returnJsonData(4001,'请填写资产方名称', []);
        }
        if (empty($post_data['accessType'])) {
            AjaxTool::returnJsonData(4001,'接入类型不能为空' ,[]);
        }
        if($post_data['accessType'] == 2){
            if (empty($post_data['appid'])) {
                AjaxTool::returnJsonData(4001,'接入类型为接口交互时appid不能为空' ,[]);
            }
            if (empty($post_data['whiteIp'])) {
                AjaxTool::returnJsonData(4001,'接入类型为接口交互时访问白名单不能为空' ,[]);
            }
        }

        $ts = time();
        if ($id) {
            $post_data['updateTime'] = $ts;
            $assets = Module_InfoBase_Assets::getInstance()->getItemById($id);
            //appid 发生了修改,
            if(($assets['accessType'] ==1 && $post_data['accessType'] == 2) || empty($assets['publicKey'])){
                $publicKey = md5($post_data['appid'].$ts);
                $post_data['publicKey'] = $publicKey;
            }
            $res = Module_InfoBase_Assets::getInstance()->update($post_data, $id);
        } else {

            $post_data['addTime'] = $ts;
            if($post_data['accessType']==2){
                if (!empty($post_data['appid'])) {
                    $publicKey = md5($post_data['appid'].$ts);
                    $post_data['publicKey'] = $publicKey;
                }
            }
            $post_data['updateTime'] = $ts;

            $res = Module_InfoBase_Assets::getInstance()->insert($post_data);
        }
        $msg = $id ? '编辑' : '添加';

        if (!$res) {
            AjaxTool::returnJsonData(400,$msg.'失败',[]);
        }
        AjaxTool::returnJsonData(200,$msg.'成功',[]);
    }

    /**
     * 编辑
     */
    public function editAction() {
        $id = $this->getRequest()->getQuery('id', 0);
        if (!$id) {
            $this->displayError('非法参数', '/assets/index');
        }
        $data = Module_InfoBase_Assets::getInstance()->getItemById($id);

        if (!$data) {
            $this->displayError('不存在该资产方信息', '/assets/index');
        }
        SmartyAdapter::assign('callNum', Module_Mediate_Workernumber::$callNum[1]);
        SmartyAdapter::assign('callNum2', Module_Mediate_Workernumber::$callNum[2]);
        SmartyAdapter::assign('numCard', Module_Mediate_Workernumber::$numCard);
        SmartyAdapter::assign('smsSign', Lib_SMS_Config::$sms_sign);
        SmartyAdapter::assign('centerName', Lib_SMS_Config::$centen_name);
        SmartyAdapter::assign('info', $data);
        SmartyAdapter::$templateName = 'assets/addandedit';

    }

}